<?php
/**
 * application router
 */
class Core_Controller_Router {
    
    /**
     * autoloader
     * 
     * @param type $className
     */
    public function autoload($className){
        
        $classPath = $this->classPath($className);
        //$this->classLoad($classPath);
    }
    
    /**
     * compose and get file
     * 
     * @param type $className
     */
    public function classPath($className){
        
        list($filename, $tier, $functionality) = preg_split('/_/', strrev($className), 3);
        $functionality = strtolower(strrev($functionality));
        $filename = strtolower(strrev($filename));
        $tier = strtolower(strrev($tier));
        
        //print_r($tier);
        //print_r($functionality);
        //print_r($filename);
        
        $folder = $this->getFolder($tier);
        $filePath = SERVER_ROOT . $functionality . "/" . $folder . "/" .$filename . ".php";
        
        //print_r($filePath);
        $this->getFilePath($filePath);
        
    }
    
    public function getFilePath($filePath){
        
        try {
            
            if(file_exists($filePath)){
                
                require_once $filePath;
                
            }
            else {
                
                throw new Exception("File '$filePath' does not exists!");
            
                
            }
            
        } catch (Exception $ex) {
            
            echo $ex->getMessage();

        }
        
    }
    
    /**
     * get folder
     * 
     * @param type $tier
     * @return type $folder
     * @throws Exception
     */
    public function getFolder($tier){
        try {
            
            switch($tier){
            
                case 'controller':
                    $folder = basename("controllers");
                    break;
                case 'model':
                    $folder = basename("models");
                    break;
                case 'data':
                    $folder = basename("data");
                    break;
                case 'libraries':
                    $folder = basename("libraries");
                    break;
                case 'drivers':
                    $folder = basename("drivers");
                    break;
                default:
                    throw new Exception("Invalid application tier '$tier'!"); 
            }
            
        } catch( Exception $ex){
            
            echo $ex->getMessage();
            
        }
        
        //print_r($folder);
        return $folder;
    }
    
}
?>
